package com.ipzoe.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import com.ipzoe.system.domain.Region;
import com.ipzoe.system.service.IRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.system.domain.HotCity;
import com.ipzoe.system.service.IHotCityService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 热门城市Controller
 * 
 * @author eric
 * @date 2020-08-29
 */
@RestController
@RequestMapping("/system/hotCity" )
public class HotCityController extends BaseController {

    @Autowired
    private IHotCityService iHotCityService;
    @Autowired
    private IRegionService iRegionService;

    /**
     * 查询热门城市列表
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:list')")
    @GetMapping("/list")
    public TableDataInfo list(HotCity hotCity)
    {
        startPage();
        LambdaQueryWrapper<HotCity> lqw = new LambdaQueryWrapper<HotCity>();
        List<HotCity> list = iHotCityService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出热门城市列表
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:export')" )
    @Log(title = "热门城市" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(HotCity hotCity) {
        LambdaQueryWrapper<HotCity> lqw = new LambdaQueryWrapper<HotCity>(hotCity);
        List<HotCity> list = iHotCityService.list(lqw);
        ExcelUtil<HotCity> util = new ExcelUtil<HotCity>(HotCity. class);
        return util.exportExcel(list, "hotCity" );
    }

    /**
     * 获取热门城市详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iHotCityService.getById(id));
    }

    /**
     * 新增热门城市
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:add')" )
    @Log(title = "热门城市" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotCity hotCity) {

        Region region = iRegionService.getById(hotCity.getRegionId());
        hotCity.setName(region.getName());
        hotCity.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iHotCityService.save(hotCity) ? 1 : 0);
    }

    /**
     * 修改热门城市
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:edit')" )
    @Log(title = "热门城市" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotCity hotCity) {
        return toAjax(iHotCityService.updateById(hotCity) ? 1 : 0);
    }

    /**
     * 删除热门城市
     */
    @PreAuthorize("@ss.hasPermi('system:hotCity:remove')" )
    @Log(title = "热门城市" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iHotCityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
